/**
 * Copyright 2010 The PlayN Tower Defense Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.gtug.bishkek.towerdefense.core;

import static playn.core.PlayN.*;

import org.gtug.bishkek.towerdefense.core.tower.Tower;
import playn.core.CanvasLayer;
import playn.core.GroupLayer;
import playn.core.Layer;

/**
 *
 * @TODO
 */
public class TowerWorld {

    /**
     * Place dimensions
     */
    public final static int PLACE_WIDTH = 50;
    public final static int PLACE_HEIGHT = 50;

    /**
     * @TODO
     */
    public class Place {

        public Place(float x, float y) {
            this.x = x;
            this.y = y;
        }
        /**
         * Initial x,y coordinates for {@link Place} position into transformation matrix
         */
        public float x, y;
        /**
         * Tower placed here
         */
        public Tower tower;
        /**
         * Flag for closing {@link Place} for tower
         */
        public boolean couldPlace = true;
        /**
         * Place layer
         */
        protected CanvasLayer layer;

        /**
         * @TODO
         * @return is tower could be placed here
         */
        public boolean couldPlace() {
            if (tower != null || !couldPlace) {
                return false;
            }
            return true;
        }

        /**
         * 
         * @return 
         */
        public Layer layer() {
            if (layer == null) {
                layer = graphics().createCanvasLayer(PLACE_WIDTH, PLACE_HEIGHT);
            }
            return layer;
        }
    }
    /**
     * Places into {@link TowerWorld}
     */
    Place[][] places;
    GroupLayer towersLayer;

    
    /**
     * Initialize of {@link GroupLayer} for placing towers
     */
    private void initTowersLayer() {
        towersLayer = graphics().createGroupLayer();
    }
    
    /**
     * Set {@link Place}s amount 
     * 
     * @param x amount of places by x coordinate
     * @param y amount of places by y coordinate
     */
    public void setPlacesMatrix(Place[][] places) {
        this.places = places;
    }

    /**
     * Fill world with Empty {@link Place}s
     * 
     * @param x amount of places by x coordinate
     * @param y amount of places by y coordinate
     */
    public void initPlaces(int x, int y) {
        places = new Place[x][y];
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                Place place = new Place(i * PLACE_WIDTH, j * PLACE_HEIGHT);
                towersLayer.add(place.layer());
                places[i][j] = place;
            }
        }
    }

    /**
     * Add {@link Tower} to the {@link TowerWorld#towersLayer}
     * 
     * @param tower 
     */
    public void showTower(Tower tower) {
    }

    /**
     * Removes {@link Tower} from {@link TowerWorld#towersLayer}
     * 
     * @param tower 
     */
    public void hideTower(Tower tower) {
    }

    /**
     * 
     * @param x
     * @param y
     * @param tower 
     */
    public void placeTower(int x, int y, Tower tower) {
    }

    /**
     * Try to find place by specified coordinates
     * 
     * @param x coordinate
     * @param y coordinate
     * @return found place
     */
    public Place place(float x, float y) {
        if (places == null) {
            return null;
        }
        int xIndex, yIndex;
        xIndex = (int) Math.floor(x / PLACE_WIDTH);
        yIndex = (int) Math.floor(y / PLACE_HEIGHT);
        return places[xIndex][yIndex];
    }
    
    /**
     * Get layer for placing {@link Tower}s
     * 
     * @return towers {@link GroupLayer}
     */
    public GroupLayer towersLayer() {
        if (towersLayer == null) {
            initTowersLayer();
        }
        return towersLayer;
    }
}
